<!--
 * @Date: 2019-12-23 14:39:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-08 16:05:22
 * @describe: -客源列表-
 -->
<template>
  <el-card shadow="never" class="aui-card--fill"  ref="fromc">
    <div class="mod-customer__customerhouseinfo}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" ref="froma">
        <el-row>
          <el-col :span="20">
            <el-form-item>
              类型：
              <el-select v-model="dataForm.customerType" class="FormSelect" clearable>
                <el-option label="写租" value="1"></el-option>
                <el-option label="写买" value="2"></el-option>
                <el-option label="住租" value="3"></el-option>
                <el-option label="住买" value="4"></el-option>
                <el-option label="商租" value="5"></el-option>
                <el-option label="商买" value="6"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              交易：
              <el-select v-model="dataForm.transactionType" class="FormSelect" clearable>
                <el-option label="求购" value="0"></el-option>
                <el-option label="求租" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              状态：
              <ren-select
                v-model="dataForm.state"
                dict-type="customerState"
                class="FormSelect"
                clearable
              ></ren-select>
            </el-form-item>
            <el-form-item>
              部门：
              <el-cascader
                class="FormSelect"
                v-model="deptSelectVal"
                :options="this.$store.state.DeptList"
                :show-all-levels="false"
                @change="deptIdChange"
                :props="deptProps"
                ref="myCascader"
                clearable
              ></el-cascader>
            </el-form-item>
            <el-form-item>
              人员：
              <el-select v-model="dataForm.userId" placeholder="请选择" class="FormSelect" clearable>
                <el-option
                  v-for="item in maintainList"
                  :key="item.id"
                  :label="item.realName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="dataForm.eager" true-label="1" false-label="0">急切</el-checkbox>
              <el-checkbox v-model="dataForm.visit" true-label="1" false-label="0">有带看</el-checkbox>
              <el-checkbox v-model="dataForm.selfCustomer" true-label="1" false-label="0">我的客源</el-checkbox>
            </el-form-item>
            <el-form-item style="margin-left:10px">
              查看来源：
              <el-select v-model="dataForm.self" style="width:100px">
                <el-option label="查看所有" value="1"></el-option>
                <el-option label="查看维护" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-input
              placeholder="请输入内容"
              v-model="dataForm.search"
              class="input-with-select"
              @keyup.enter.native="getDataList"
              clearable
            >
              <el-button slot="append" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              时间：
              <el-select
                v-model="dataForm.date"
                @change="dateSelectChange"
                class="FormSelect"
                clearable
              >
                <el-option label="自定义" value="6"></el-option>
                <el-option label="当天" value="1"></el-option>
                <el-option label="本周" value="2"></el-option>
                <el-option label="本月" value="3"></el-option>
                <el-option label="本季度" value="4"></el-option>
                <el-option label="今年" value="5"></el-option>
              </el-select>&nbsp;
              <el-date-picker
                v-model="dataForm.createDates"
                type="date"
                placeholder="开始时间"
                value-format="yyyy-MM-dd"
                style="width:136px"
                :disabled="dataForm.date!='6'"
              ></el-date-picker>
              {{' -'}}
              <el-date-picker
                v-model="dataForm.createDatee"
                type="date"
                placeholder="结束时间"
                value-format="yyyy-MM-dd"
                style="width:136px"
                :disabled="dataForm.date!='6'"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              区域：
              <el-select
                v-model="dataForm.districtCode"
                placeholder="请选择"
                clearable
                class="FormSelect"
              >
                <el-option
                  v-for="item in this.$store.state.CityRegion"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              价格：
              <el-input v-model="dataForm.prices" placeholder="最低价" class="Section_inp" clearable></el-input>
              {{' -'}}
              <el-input v-model="dataForm.pricee" placeholder="最高价" class="Section_inp" clearable></el-input>
            </el-form-item>
            <el-form-item>
              面积：
              <el-input v-model="dataForm.areas" placeholder="最小" class="Section_inp" clearable></el-input>
              {{' -'}}
              <el-input v-model="dataForm.areae" placeholder="最大" class="Section_inp" clearable></el-input>
            </el-form-item>
            <el-form-item>
              状态：
              <ren-select
                v-model="dataForm.state"
                dict-type="customerState"
                placeholder="状态"
                class="FormSelect"
                clearable
              ></ren-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="Reset">重置</el-button>
            </el-form-item>
            <el-form-item>
              <el-dropdown>
                <el-button type="primary">
                  添加-租
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('1')">写字楼</el-dropdown-item>
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('3')">住宅</el-dropdown-item>
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('5')">商铺</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item>
            <el-form-item>
              <el-dropdown>
                <el-button type="primary">
                  添加-买
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('2')">写字楼</el-dropdown-item>
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('4')">住宅</el-dropdown-item>
                  <el-dropdown-item style="width:80px" @click.native="addSelectChange('6')">商铺</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="$hasPermission('customer:customerinfo:delete')&&dataForm.self=='0'"
                type="primary"
                @click="deleteHandle()"
              >{{ $t('delete') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%;"
        @row-dblclick="tableRowDbclick"
        :height="autoTabHeight"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" label="编号" header-align="center" align="center">
          <template slot-scope="scope">{{"KY - "+scope.row.id.substring(scope.row.id.length-6)}}</template>
        </el-table-column>
        <el-table-column prop="customerName" label="客户姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="transactionType" label="交易类型" header-align="center" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.transactionType==0" style="color:red">求购</span>
            <span v-else style="color:blue">求租</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="物业类型" header-align="center" align="center">
          <template slot-scope="scope">
            <span
              :style="dictTypeColor(scope.row.type)"
            >{{ $getDictLabel("dictType", scope.row.type) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dictName" label="意向楼盘" header-align="center" align="center"></el-table-column>
        <el-table-column prop="districtCode" label="行政区域" header-align="center" align="center">
          <template slot-scope="scope">{{ $Dictionaries("CityRegion", scope.row.districtCode) }}</template>
        </el-table-column>
        <el-table-column
          prop="areas"
          label="面积区间（㎡）"
          header-align="center"
          align="center"
          width="120"
        >
          <template slot-scope="scope">{{scope.row.areas}} - {{scope.row.areae}}</template>
        </el-table-column>
        <el-table-column
          prop="prices"
          label="价格区间"
          header-align="center"
          align="center"
          width="120"
        >
          <template slot-scope="scope">{{scope.row.prices}} - {{scope.row.pricee}}</template>
        </el-table-column>
        <el-table-column prop="renovation" label="装修要求" header-align="center" align="center">
          <template slot-scope="scope">{{ $getDictLabel("dictRenovation", scope.row.renovation) }}</template>
        </el-table-column>
        <!-- <el-table-column prop="purpose" label="房源用途" header-align="center" align="center"></el-table-column> -->

        <el-table-column prop="communication" label="沟通阶段" header-align="center" align="center">
          <template
            slot-scope="scope"
          >{{ $getDictLabel("customerCommunication", scope.row.communication) }}</template>
        </el-table-column>
        <el-table-column prop="state" label="状态" header-align="center" align="center">
          <template slot-scope="scope">{{ $getDictLabel("customerState", scope.row.state) }}</template>
        </el-table-column>
        <el-table-column prop="source" label="客户来源" header-align="center" align="center">
          <template slot-scope="scope">{{ $getDictLabel("customerSource", scope.row.source) }}</template>
        </el-table-column>
        <el-table-column prop="maintainName" label="经纪人" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createDate" label="录入时间" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.createDate!=null?scope.row.createDate.substring(0,10):scope.row.createDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="examine"
          label="审核"
          header-align="center"
          align="center"
          v-if="dataForm.self=='0'"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.examine"
              @change="tabExamineChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
              :disabled="!$hasPermission('customer:customerinfo:updateExamine')"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="followTime"
          label="最新跟进"
          header-align="center"
          align="center"
          width="100"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{scope.row.followTime?scope.row.followTime.substring(0,10):scope.row.followTime}}</span>
          </template>
        </el-table-column>0
      </el-table>
     
      <div class="paginationBox">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        ></el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> -->
      <component v-if="addOruUpdate" :is="zjName" @CloseDialog="CloseDialogFun"></component>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
// import AddOrUpdate from './customerhouseinfo-add-or-update'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        createdIsNeed: false,
        getDataListURL: "/customer/customerinfo/housePage",
        getDataListIsPage: true,
        exportURL: "/customer/customerhouseinfo/export",
        deleteURL: "/customer/customerhouseinfo",
        deleteIsBatch: true
      },
      dataForm: {
        id: this.$route.query.moduleId ? this.$route.query.moduleId : "",
        customerType: "",
        transactionType: "",
        selfCustomer: "",
        state: "1",
        deptId: "",
        eager: "",
        visit: "",
        districtCode: "",
        prices: "",
        pricee: "",
        areas: "",
        areae: "",
        userId: this.$route.query.userId ? this.$route.query.userId : "",
        createDates: "",
        createDatee: "",
        date: "6",
        customerName: "",
        self: "1",
        search: "",
        dictId: this.$route.query.dictId ? this.$route.query.dictId : "",
        customerId: this.$route.query.customerId
          ? this.$route.query.customerId
          : ""
      },
      maintainList: this.$store.state.maintainList,
      select_type: "1",
      addSelect: "",
      addOruUpdate: false,
      addUrl: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      deptSelectVal: "",
      autoTabHeight: 700
    };
  },
  components: {
    // AddOrUpdate
  },
  computed: {
    zjName: {
      get: function() {
        this.addUrl;
        var myComponent = () => import(`${this.addUrl}`);
        return myComponent;
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.autoWidth();
    });
    if (this.$route.query.statisticType) {
      this.statisticOpen();
    } else {
      this.getDataList();
    }
  },
  mounted() {
    let _this = this;
    window.onresize = function() {
      _this.autoWidth();
    };
  },
  methods: {
    autoWidth() {
      let that = this;
      let formh = that.$refs.froma.$el.offsetHeight;
      let h = that.$refs.fromc.$el.offsetHeight;
      let heightStyle = h - formh - 45;
      that.autoTabHeight = heightStyle;
    },
    //统计跳转
    statisticOpen() {
      if (this.$route.query.customerModuleId == 1) {
        let statisticType = this.$route.query.statisticType;
        if (statisticType == 1) {
          this.dataForm.userId = this.$route.query.id;
        } else {
          this.deptSelectVal = this.$route.query.id;
          this.dataForm.deptId = this.$route.query.id;
        }
        this.dataForm.date = this.$route.query.date;
        this.dataForm.createDates = this.$route.query.createDates;
        this.dataForm.createDatee = this.$route.query.createDatee;
      }
    },
    Reset() {
      this.dataForm = {
        id: this.$route.query.moduleId ? this.$route.query.moduleId : "",
        customerType: "",
        transactionType: "",
        selfCustomer: "",
        state: "1",
        deptId: "",
        eager: "",
        visit: "",
        districtCode: "",
        prices: "",
        pricee: "",
        areas: "",
        areae: "",
        userId: "",
        createDates: "",
        createDatee: "",
        date: "6",
        customerName: "",
        self: "1",
        search: "",
        dictId: this.$route.query.dictId ? this.$route.query.dictId : "",
        customerId: this.$route.query.customerId
          ? this.$route.query.customerId
          : ""
      };
    },
    addSelectChange(val) {
      this.addOruUpdate = true;
      if (val == "6") {
        //买商
        this.addUrl = "./page/customermsinfo-add-or-update";
      } else if (val == "2") {
        //买写
        this.addUrl = "./page/customermxinfo-add-or-update";
      } else if (val == "4") {
        //买住
        this.addUrl = "./page/customermzinfo-add-or-update";
      } else if (val == "5") {
        //租商
        this.addUrl = "./page/customerzsinfo-add-or-update";
      } else if (val == "1") {
        //租写
        this.addUrl = "./page/customerzxinfo-add-or-update";
      } else if (val == "3") {
        //租住
        this.addUrl = "./page/customerzzinfo-add-or-update";
      }
    },
    CloseDialogFun() {
      this.addOruUpdate = false;
    },
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    // 日期方式切换
    dateSelectChange(val) {
      if (val != "6") {
        this.dataForm.createDates = "";
        this.dataForm.createDatee = "";
      }
    },
    //搜索
    // searchClick() {
    //   if (this.select_type == "1") {
    //     this.dataForm.customerName = this.searchVal;
    //   } else if (this.select_type == "2") {
    //     this.dataForm.customerName = "";
    //   }
    //   this.getDataList();
    // },
    //行双击
    tableRowDbclick(row) {
      this.$router.push({
        name: "customerPage",
        query: {
          ID: row.id,
          customerType: row.customerType
        }
      });
    },
    //审核
    tabExamineChange(row) {
      this.$http
        .put(
          `/customer/customerinfo/updateExamine`,
          {},
          {
            params: {
              id: row.id,
              customerType: row.customerType,
              examine: row.examine
            }
          }
        )
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("更改成功");
        })
        .catch(() => {});
    }
  },
  watch: {
    dataForm: {
      handler(nowVal, oldVal) {
        this.getDataList();
      },
      deep: true
    },
    $route(to, from) {
      if (to.name == "customer-customerhouseinfo") {
        if (this.$route.query.customerModuleId == 1) {
          let statisticType = this.$route.query.statisticType;
          if (statisticType == 1) {
            this.dataForm.userId = this.$route.query.id;
          } else {
            this.deptSelectVal = this.$route.query.id;
            this.dataForm.deptId = this.$route.query.id;
          }
          this.dataForm.state = this.$route.query.state;
          this.dataForm.date = this.$route.query.date;
          this.dataForm.createDates = this.$route.query.createDates;
          this.dataForm.createDatee = this.$route.query.createDatee;
        } else if (this.$route.query.moduleId) {
          this.dataForm.id = this.$route.query.moduleId;
        } else if (this.$route.query.userId) {
          this.dataForm.userId = this.$route.query.userId;
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.FormSelect {
  width: 90px;
}
.Section_inp {
  width: 80px !important;
}
.el-form--inline .el-form-item {
  margin-right: 7px;
}
.el-checkbox {
  margin-right: 10px;
}
.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
  margin-bottom: 7px;
}
</style>